<?php
include_once('./config/config.php');

// 判断有没有表单提交
if ($_POST) {
  // 接收全部参数
  $id = $_POST['depid'];
  $name = $_POST['name'];
  $action = $_POST['action'];
  if ($action == 'find') {
    $sql = "select * from job where name = '$name'";
    $res = findOne($sql);
    if ($res) {
      error("该职位已存在", $res);
    }
    success("该职位可以使用", $res);
  }
  // 封装数据
  $data = [
    'name' => $_POST['jobname'],
    'depid' => $_POST['depid']
  ];
  var_dump($data);
  $res = insert('job', $data);
  if ($res) {
    ShowMsg("新增职位成功", 'jobList.php');
  } else {
    ShowMsg("新增职位失败", 'jobList.php');
  }

}
$sql = "select * from department";
$dep = findAll($sql);   // 获取部门列表
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<body>
  <!--<![endif]-->

  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>

  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加职位</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">addJob</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i>  返回列表</button>
          <div class="btn-group">
          </div>
        </div>
        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">
                <!-- 职位名称 -->
                <label>职位名称</label>
                <input type="text" class="input-xxlarge" class="jname" name="jobname" placeholder="请输入职位名称" required>
                <span id="msg"></span>
                <!-- 选择部门 -->
                <label>选择部门</label>
                <select name="depid" id="">
                  <?php foreach($dep as $item) { ?>
                    <option value="<?php echo $item['id']; ?>"> <?php echo $item['name'] ?> </option>
                  <?php }?>
                </select>
                <br>
                <button class="btn btn-primary" type="submit" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>
        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>
<script>
    $('input[name="jobname"]').change(function () {  
      let name = $(this).val();
      $.ajax({
        type: 'post',
        url: 'addJob.php',
        data: {
          name,
          action: 'find'
        },
        dataType: 'json',
        success: function(res) {
          if (res.code == 1) {
            $('#msg').text(res.msg).css({"color": "blue"});
          } else {
            $('#msg').text(res.msg).css({"color": "red"});
          }
        },
        error: function (err) { 
          console.log("err")
        }
      })
    })

</script>
